package web.springboot.controller;

import custom.TestCustom;
import custom.TestCustomValidHandle;
import custom.TestParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lingkang.finalvalidated.core.FinalValidator;
import top.lingkang.finalvalidated.core.ValidObject;
import web.springboot.param.BigDecimalParam;
import web.springboot.param.EmailParam;
import web.springboot.param.ErParam;
import web.springboot.param.IntegerParam;
import web.springboot.param.LoginParam;
import web.springboot.param.MyGroupsParam;
import web.springboot.param.NotNullParam;
import web.springboot.param.NullParam;
import web.springboot.param.PatternParam;
import web.springboot.param.StringParam;

/**
 * @author lingkang
 * Created by 2024/1/26
 */
@Slf4j
@RestController
public class ApiController {
    @RequestMapping("/")
    public String index(LoginParam param) {
        return "index " + param;
    }

    @RequestMapping("/login")
    public Object login(@ValidObject LoginParam param) {
        return param;
    }

    @RequestMapping("/integer")
    public Object integer(@ValidObject IntegerParam param) {
        return param;
    }

    @RequestMapping("/string")
    public Object string(@ValidObject StringParam param) {
        return param;
    }

    @RequestMapping("/bigDecimal")
    public Object string(@ValidObject BigDecimalParam param) {
        return param;
    }

    @RequestMapping("/email")
    public Object email(@ValidObject EmailParam param) {
        return param;
    }

    @RequestMapping("/pattern")
    public Object pattern(@ValidObject PatternParam param) {
        return param;
    }

    @RequestMapping("/custom")
    public Object custom(TestParam param) {
        FinalValidator.valid(param);
        return param;
    }

    @RequestMapping("/null")
    public Object aNull(@ValidObject NullParam param) {
        return param;
    }

    @RequestMapping("/notNull")
    public Object notNull(@ValidObject NotNullParam param) {
        return param;
    }

    @RequestMapping("er")
    public Object er(@ValidObject ErParam param) {
        return param;
    }

    @RequestMapping("/groups")
    public Object groups(@ValidObject(MyGroupsParam.Create.class) MyGroupsParam param) {
        // 也可以使用spring的 @Validated(MyGroupsParam.Create.class) 注解
        log.info("groups-param  {}", param);
        return "groups: " + param;
    }

    @RequestMapping("/addCustom")
    public Object addCustom() {
        FinalValidator.addCustom(TestCustom.class, TestCustomValidHandle.class);
        return "ok";
    }

    @RequestMapping("/useCustom")
    public Object useCustom(@ValidObject TestParam param) {
        return "param: " + param;
    }
}
